/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SWANLINK_FINANCE_CARDSENDER_INTERFACES_INNERKITS_NATIVE_INCLUDE_PARCELABLE_VECTOR_RW_H
#define SWANLINK_FINANCE_CARDSENDER_INTERFACES_INNERKITS_NATIVE_INCLUDE_PARCELABLE_VECTOR_RW_H

#include <typeinfo>
#include <parcel.h>
#include <hilog_wrapper.h>

namespace SwanLink {
namespace Finance {

template<typename T>
bool ReadVectorFromParcel(OHOS::Parcel& parcel, std::vector<T>& vectors)
{
    int size = 0;
    if (!parcel.ReadInt32(size)) {
        return false;
    }
    vectors.clear();
    for (int i = 0; i < size; i++) {
        T t;
        const uint8_t *buffer = parcel.ReadBuffer(sizeof(T));
        if (buffer == nullptr)
            return false;
        memcpy(&t, buffer, sizeof(T));
        vectors.emplace_back(t);
    }
    return true;
}

template<typename T>
bool WriteVectorToParcel(OHOS::Parcel& parcel, const std::vector<T>& vectors)
{
    size_t size = vectors.size();
    if (!parcel.WriteInt32(size)) {
        return false;
    }
    for (size_t i = 0; i < size; i++) {
        if (!parcel.WriteBuffer(&vectors[i], sizeof(T))) {
            return false;
        }
    }
    return true;
}
}  // namespace Finance
}  // namespace SwanLink
#endif  // SWANLINK_FINANCE_CARDSENDER_INTERFACES_INNERKITS_NATIVE_INCLUDE_PARCELABLE_VECTOR_RW_H
